import ElementUI from 'element-ui';

// 验证表格里面的每一项
async function tableListCheck(tableList) {
  let ifCheckAll = true; //是否检查完了全部(每一个参会人必填项信息)
  for (let i = 0; i < tableList.length; i++) {
    let res = await checkFormFiled(tableList[i], i + 1);
    if (!res) {
      ifCheckAll = false;
      break;
    }
  }
  return new Promise((resolve, reject) => {
    resolve(ifCheckAll)
  });
}



//检验表单字段是否正确(是否有空，必填项是否填了等)
async function checkFormFiled(formFiled, index) {
  let arr = formFiled.componenList;
  let ifCheckAll = true; //是否检查完了全部
  for (let i = 0; i < arr.length; i++) {
    let result = await chenkFiledValue(arr[i], formFiled.title);
    if (result == "break") {
      ifCheckAll = false;
      break;
    }
  }
  return new Promise((resolve, reject) => {
    resolve(ifCheckAll)
  });
}
// 检测字段值是否允许为空
function chenkFiledValue(item, formFiledTit) {
  return new Promise((resolve, reject) => {
    if (item.attribute.ifCheck && item.attribute.filedValue === "") {
      // if (item.attribute.ifCheck && item.attribute.filedValue === "" && item.attribute.filedCode!='bbsId') {
      ElementUI.Message.warning({
        message: `${formFiledTit}请填写正确的${item.attribute.titName}`,
        type: "warning"
      });
      resolve("break");
    } else {
      resolve("next");
    }
  });
}


export {
  tableListCheck
};
